package com.thinkingstar.iads.common.utils;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by sy on 2017/7/19.
 */
public class PublicRequestUtil {
	
    /**
     * 发送  http-post 请求的公共方法
     * 用于调用restful接口
     * @param url
     * @param jsonObject
     * @return
     * @since JDK 1.6
     */
    public static String setPostRequestURL(String url, JSONObject jsonObject){
        String response_content_string = ""; // 接收请求的返回值
        // 创建默认的httpClient实例
        CloseableHttpClient httpclient = new DefaultHttpClient();
        HttpPost http_post = new HttpPost(url);
        // 创建参数队列
        List<NameValuePair> paramList = new ArrayList<NameValuePair>();
        if(jsonObject != null){
            for(String key : jsonObject.keySet()){
                paramList.add(new BasicNameValuePair(key, (String) jsonObject.get(key)));
            }
        }
        try {
            // 执行post请求
            UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(paramList, "UTF-8");
            http_post.setEntity(uefEntity);
            CloseableHttpResponse resp = httpclient.execute(http_post);
            HttpEntity httpEntity = resp.getEntity();
            response_content_string = EntityUtils.toString(httpEntity);
            resp.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return response_content_string;
    }
	
	
    /**
     * 发送  http-post 请求的公共方法
     * 用于调用restful接口
     * @param url
     * @param paramMap
     * @return
     * @since JDK 1.6
     */
    public static String setPostRequestURL(String url, HashMap<String, String> paramMap){
        String response_content_string = ""; // 接收请求的返回值
        // 创建默认的httpClient实例
        CloseableHttpClient httpclient = new DefaultHttpClient();
        HttpPost http_post = new HttpPost(url);
        // 创建参数队列
        List<NameValuePair> paramList = new ArrayList<NameValuePair>();
        if(paramMap != null){
            for(String key : paramMap.keySet()){
                paramList.add(new BasicNameValuePair(key, paramMap.get(key)));
            }
        }
        try {
            // 执行post请求
            UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(paramList, "UTF-8");
            http_post.setEntity(uefEntity);
            CloseableHttpResponse resp = httpclient.execute(http_post);
            HttpEntity httpEntity = resp.getEntity();
            response_content_string = EntityUtils.toString(httpEntity);
            resp.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return response_content_string;
    }

    /**
     * 发送  http-get 请求的公共方法
     * 用于调用restful接口
     * @param url
     * @return
     * @since JDK 1.6
     */
    public static String setGetRequestURL(String url){
        String response_content_string = ""; // 接收请求的返回值
        // 创建默认的httpClient实例
        CloseableHttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(url);
        try {
            // 执行get请求
            CloseableHttpResponse resp = httpclient.execute(httpget);
            HttpEntity httpEntity = resp.getEntity();
            response_content_string = EntityUtils.toString(httpEntity);
            resp.close();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return response_content_string;
    }
}
